package com.mondo.financialwallet.presentation.utils;

import android.graphics.Color;
import android.graphics.DashPathEffect;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.mondo.financialwallet.presentation.model.ProductProfitDataModel;

import java.util.ArrayList;
import java.util.List;

public class LineChartUtils {

    public static void init(String label, LineChart lineChart, List<ProductProfitDataModel> dataList){
        lineChart.setBackgroundColor(Color.WHITE);
        lineChart.getDescription().setEnabled(false);
        lineChart.setTouchEnabled(true);
        lineChart.setDragEnabled(true);
        lineChart.setHighlightPerTapEnabled(false);
        lineChart.setHighlightPerDragEnabled(false);
        lineChart.setDoubleTapToZoomEnabled(false);

        // 初始化X轴
        XAxis x = lineChart.getXAxis();
        x.setEnabled(true);
        x.setTextSize(9F);
        x.setPosition(XAxis.XAxisPosition.BOTTOM);
        x.setDrawGridLines(true);
        x.setDrawAxisLine(true);
        x.setGridLineWidth(0);
        x.setGridColor(Color.TRANSPARENT);
        x.setAxisLineColor(Color.TRANSPARENT);
        x.setValueFormatter((value, axis) -> {
            try{
                return dataList.get((int)value).getDate();
            }catch (Exception e){
                return "";
            }
        });

        // 初始化Y轴
        YAxis y = lineChart.getAxisLeft();
        y.removeAllLimitLines();
        y.setGridDashedLine(new DashPathEffect(new float[]{10, 10}, 0));
        y.setGridColor(Color.parseColor("#CCCCCC"));
        y.setDrawLimitLinesBehindData(false);

        // 图形
        lineChart.getAxisRight().setEnabled(false);
        lineChart.getLegend().setForm(Legend.LegendForm.SQUARE);
        lineChart.animateX(1500);

        // 设置数据
        setChartData(label, lineChart, dataList);
        lineChart.setVisibleXRange(0, 6);
    }

    private static void setChartData(String label, LineChart lineChart, List<ProductProfitDataModel> dataList) {
        ArrayList<Entry> values = new ArrayList<>();

        for (int i = 0; i < dataList.size(); i++) {
            values.add(new Entry(i, dataList.get(i).getProfit()));
        }

        LineDataSet set1;
        if (lineChart.getData() != null &&
                lineChart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            lineChart.getData().notifyDataChanged();
            lineChart.notifyDataSetChanged();
        } else {
            set1 = new LineDataSet(values, label);
            set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            set1.setLineWidth(1.0f);
            set1.setColor(Color.parseColor("#3F82DD"));
            set1.setFillColor(Color.parseColor("#3F82DD"));
            set1.setCircleRadius(3);
            set1.setCircleHoleRadius(2.5F);
            set1.setCircleColorHole(Color.WHITE);
            set1.setFillAlpha(100);
            set1.setDrawFilled(true);
            set1.setDrawValues(false);
            set1.setFillFormatter((dataSet, dataProvider) ->
                    lineChart.getAxisLeft().getAxisMinimum());

            // 设置数据
            lineChart.setData(new LineData(set1));
        }
    }
}
